package com.wxzz.elearing.comment.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wxzz.elearing.comment.dto.ApplyStudyTitleTypeDto;
import com.wxzz.elearing.comment.entity.ApplyStudyTitleType;
import com.wxzz.elearing.comment.mapper.ApplyStudyTitleTypeMapper;
import com.wxzz.elearing.comment.mapper.StudyQuestionsAnswersListMapper;
import com.wxzz.elearing.comment.service.IApplyStudyTitleTypeService;
import com.wxzz.elearning.common.exception.ServiceException;
import com.wxzz.elearning.common.vo.JgPageVo;
import com.wxzz.elearning.common.vo.ReturnVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author
 * @since 2021-05-18
 */
@Service
public class ApplyStudyTitleTypeServiceImpl extends ServiceImpl<ApplyStudyTitleTypeMapper, ApplyStudyTitleType> implements IApplyStudyTitleTypeService {

    @Resource
    private ApplyStudyTitleTypeMapper applyStudyTitleTypeMapper;

    public ReturnVO saveApplyType(ApplyStudyTitleType applyStudyTitleType){
        if(applyStudyTitleType.getId()!=null){
            applyStudyTitleTypeMapper.updateById(applyStudyTitleType);
            return new ReturnVO("修改类型成功",null);
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("type_name",applyStudyTitleType.getTypeName());
        queryWrapper.eq("enterprise_id",applyStudyTitleType.getEnterpriseId());
        Integer integer = applyStudyTitleTypeMapper.selectCount(queryWrapper);
        if(integer>0){
            throw new ServiceException("该标题类型已存在");
        }
        applyStudyTitleTypeMapper.insert(applyStudyTitleType);
        return new ReturnVO("添加类型成功",null);
    }

    @Resource
    private StudyQuestionsAnswersListMapper studyQuestionsAnswersListMapper;
    public ReturnVO deleteApplyType(ApplyStudyTitleType applyStudyTitleType){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("title_type",applyStudyTitleType.getId());
        Integer count = studyQuestionsAnswersListMapper.selectCount(queryWrapper);
        if(count!=0){
            return new ReturnVO("此类型有文章使用中，如需删除请先删除相关文章！",null);
        }
        applyStudyTitleTypeMapper.deleteById(applyStudyTitleType.getId());
        return new ReturnVO("类型删除成功！",null);
    }

    public ReturnVO applyTypeList(ApplyStudyTitleTypeDto applyStudyTitleType){
        PageHelper.startPage(applyStudyTitleType.getCurPage(), applyStudyTitleType.getMaxPage());
        QueryWrapper queryWrapper = new QueryWrapper();
        if(StringUtils.isNotBlank(applyStudyTitleType.getTypeName())){
            queryWrapper.eq("type_name",applyStudyTitleType.getTypeName());
        }
        queryWrapper.eq("enterprise_id",applyStudyTitleType.getEnterpriseId());
        List<ApplyStudyTitleType> list = applyStudyTitleTypeMapper.selectList(queryWrapper);
        list.forEach(model->{
            QueryWrapper queryWrapper1 = new QueryWrapper();
            queryWrapper1.eq("title_type",model.getId());
            Integer integer = studyQuestionsAnswersListMapper.selectCount(queryWrapper1);
            model.setImproveCount(integer);
        });
        return new ReturnVO("类型查询成功", new JgPageVo<>(new PageInfo(list)));

    }
}
